package com.example.controller;

import com.example.service.BorderSize;

import java.util.HashMap;

public class AI implements BorderSize {
    static HashMap<String,Integer> map = new HashMap<String,Integer>();
    static {
        //防守
        map.put("0", 1);
        map.put("010",10);
        map.put("021", 15);
        map.put("0112",20);
        map.put("0221",150);
        map.put("02221",400);
        map.put("01112",500);
        map.put("0110", 800);
        map.put("022221", 5000);
        map.put("01110", 2000);
        map.put("011112", 7000);
        map.put("02020", 300);
        map.put("02220", 5000);
        map.put("020", 25);
        map.put("0220",280);
        map.put("022220",100000);
        map.put("01111", 4000);
        map.put("02", 5);


    }

    //判断每个点的权值
    public void AIplay() {
        for (int i = 0; i < ROW; i++) {
            for (int j = 0; j < COLUM; j++) {
                weightArray[i][j] = 0;
            }
        }
        for (int r = 0; r < ROW; r++) {
            for (int c = 0; c < COLUM; c++) {
                if (chessArray[r][c] == 0) {        //判断该位是否为空位
                    //水平方向
                    String code = "0";    //用来记录棋子相连的情况
                    int chess = 0;   //记录第一次出现的棋子
                    int number = 0;    //记录空位出现的次数
                    //水平向左统计
                    for (int i = c - 1; i >= 0; i--) {
                        if (chessArray[r][i] == 0) {//判断是否为空位
                            if (c == i + 1) {
                                break;    //如果有两个连续的空位，跳出
                            } else if (number == 0) {    //第一次出现空位
                                code = code + chessArray[r][i];    //记录棋子相连的情况
                                number++;
                            } else if (number == 1) {    //第二次出现空位
                                if (chessArray[r][i] == chessArray[r][i + 1]) {    //判断是否又为2个连续的空位
                                    break;
                                }
                                code = code + chessArray[r][i];
                                number++;
                            } else if (number == 2) {    //第三次出现空位
                                if (chessArray[r][i] == chessArray[r][i + 1]) {    //空位连续
                                    break;
                                }
                            }
                        } else {        //该位是棋子
                            if (chess == 0) {    //第一次出现棋子
                                chess = chessArray[r][i];
                                code = code + chessArray[r][i];
                            } else if (chess == chessArray[r][i]) {
                                code = code + chessArray[r][i];
                            } else {
                                code = code + chessArray[r][i];
                                break;
                            }
                        }
                    }
                    System.out.print(code + " ");
                    Integer value0 = map.get(code);    //Inter 整数类
                    if (value0 != null) {
                        weightArray[r][c] += value0;
                    }
                    code = "0";
                    chess = 0;
                    number = 0;
                    //水平向右统计
                    for (int i = c + 1; i < COLUM; i++) {
                        if (chessArray[r][i] == 0) {
                            if (c == i - 1) {
                                break;    //相邻2空位，跳出
                            } else if (number == 0) {    //第一次出现空位
                                code = code + chessArray[r][i];
                                number++;
                            } else if (number == 1) {
                                //第2次出现空位
                                if (chessArray[r][i] == chessArray[r][i - 1]) {
                                    break;        //连续2空位跳出
                                } else {
                                    code = code + chessArray[r][i];
                                    number++;
                                }
                            } else if (number == 2) {    //第3次出现空位
                                if (chessArray[r][i] == chessArray[r][i - 1]) {
                                    break;
                                }
                            }
                        } else {    //该位位棋子
                            if (chess == 0) {
                                chess = chessArray[r][i];
                                code = code + chessArray[r][i];
                            } else if (chess == chessArray[r][i]) {
                                code = code + chessArray[r][i];
                            } else {            //碰到不同颜色的棋子跳出循环
                                code = code + chessArray[r][i];
                                break;
                            }
                        }
                    }
                    System.out.print(code + " ");
                    Integer value1 = map.get(code);    //Inter 整数类
                    if (value1 != null) {
                        weightArray[r][c] += value1;
                    }
                    //垂直方向
                    code = "0";
                    chess = 0;
                    number = 0;
                    //垂直向上
                    for (int i = r - 1; i >= 0; i--) {
                        if (chessArray[i][c] == 0) {
                            if (r == i + 1) {
                                break;        //连续2个空位跳出循环
                            } else if (number == 0) {    //第一次出现空位
                                code = code + chessArray[i][c];
                                number++;
                            } else if (number == 1) {//第2次出现空位
                                if (chessArray[i][c] == chessArray[i + 1][c]) {
                                    break;
                                } else {
                                    code = code + chessArray[i][c];
                                    number++;
                                }
                            } else if (number == 2) {//第3次出现空位
                                if (chessArray[i][c] == chessArray[i + 1][c]) {
                                    break;
                                }
                            }
                        } else {
                            if (chess == 0) {
                                chess = chessArray[i][c];
                                code = code + chessArray[i][c];
                            } else if (chess == chessArray[i][c]) {
                                code = code + chessArray[i][c];
                            } else {
                                code = code + chessArray[i][c];
                                break;
                            }
                        }
                    }
                    System.out.print(code + " ");
                    Integer value2 = map.get(code);    //Inter 整数类
                    if (value2 != null) {
                        weightArray[r][c] += value2;
                    }
                    code = "0";
                    chess = 0;
                    number = 0;
                    //垂直向下
                    for (int i = r + 1; i < ROW; i++) {
                        if (chessArray[i][c] == 0) {
                            if (r == i - 1) {
                                break;
                            } else if (number == 0) {
                                code = code + chessArray[i][c];
                                number++;
                            } else if (number == 1) {
                                if (chessArray[i][c] == chessArray[i - 1][c]) {
                                    break;
                                } else {
                                    code = code + chessArray[i][c];
                                }
                            } else if (number == 2) {
                                if (chessArray[i][c] == chessArray[i - 1][c]) {
                                    break;
                                }
                            }
                        } else {
                            if (chess == 0) {
                                chess = chessArray[i][c];
                                code = code + chessArray[i][c];
                            } else if (chess == chessArray[i][c]) {
                                code = code + chessArray[i][c];
                            } else {
                                code = code + chessArray[i][c];
                                break;
                            }
                        }
                    }
                    Integer value3 = map.get(code);    //Inter 整数类
                    if (value3 != null) {
                        weightArray[r][c] += value3;
//						}
                        //左斜
                        code = "0";
                        chess = 0;
                        number = 0;
                        //往左上
                        int i, j;
                        for (i = r - 1, j = c - 1; i >= 0 && j >= 0; i--, j--) {
                            if (chessArray[i][j] == 0) {
                                if (r == i + 1 && c == j + 1) {
                                    break;
                                } else if (number == 0) {
                                    code = code + chessArray[i][j];
                                    number++;
                                } else if (number == 1) {
                                    if (chessArray[i][j] == chessArray[i + 1][j + 1]) {
                                        break;
                                    } else {
                                        code = code + chessArray[i][j];
                                        number++;
                                    }
                                } else if (number == 2) {
                                    if (chessArray[i][j] == chessArray[i + 1][j + 1]) {
                                        break;
                                    }
                                }
                            } else {
                                if (chess == 0) {
                                    chess = chessArray[i][j];
                                    code = code + chessArray[i][j];
                                } else if (chess == chessArray[i][j]) {
                                    code = code + chessArray[i][j];
                                } else {
                                    code = code + chessArray[i][j];
                                    break;
                                }
                            }
                        }
                        System.out.println(code + " ");
                        Integer value4 = map.get(code);    //Inter 整数类
                        if (value4 != null) {
                            weightArray[r][c] += value4;
                        }
                        code = "0";
                        chess = 0;
                        number = 0;
                        //往右下
                        for (i = r + 1, j = c + 1; i < ROW && j < COLUM; i++, j++) {
                            if (chessArray[i][j] == 0) {
                                if (r == i - 1 && c == j - 1) {
                                    break;
                                } else if (number == 0) {
                                    code = code + chessArray[i][j];
                                    number++;
                                } else if (number == 1) {
                                    if (chessArray[i][j] == chessArray[i - 1][j - 1]) {
                                        break;
                                    } else {
                                        code = code + chessArray[i][j];
                                        number++;
                                    }
                                } else if (number == 2) {
                                    if (chessArray[i][j] == chessArray[i - 1][j - 1]) {
                                        break;
                                    }
                                }
                            } else {
                                if (chess == 0) {
                                    chess = chessArray[i][j];
                                    code = code + chessArray[i][j];
                                } else if (chess == chessArray[i][j]) {
                                    code = code + chessArray[i][j];
                                } else {
                                    code = code + chessArray[i][j];
                                    break;
                                }
                            }
                        }
                        System.out.println(code + " ");
                        Integer value5 = map.get(code);    //Inter 整数类
                        if (value5 != null) {
                            weightArray[r][c] += value5;
                        }
                        //右斜
                        code = "0";
                        chess = 0;
                        number = 0;
                        //往左
                        for (i = r + 1, j = c - 1; i < ROW && j >= 0; i++, j--) {
                            if (chessArray[i][j] == 0) {
                                if (r == i - 1 && c == j + 1) {
                                    break;
                                } else if (number == 0) {
                                    code = code + chessArray[i][j];
                                    number++;
                                } else if (number == 1) {
                                    if (chessArray[i][j] == chessArray[i - 1][j + 1]) {
                                        break;
                                    } else {
                                        code = code + chessArray[i][j];
                                        number++;
                                    }
                                } else if (number == 2) {
                                    if (chessArray[i][j] == chessArray[i - 1][j + 1]) {
                                        break;
                                    }
                                }
                            } else {
                                if (chess == 0) {
                                    chess = chessArray[i][j];
                                    code = code + chessArray[i][j];
                                } else if (chess == chessArray[i][j]) {
                                    code = code + chessArray[i][j];
                                } else {
                                    code = code + chessArray[i][j];
                                    break;
                                }
                            }
                        }
                        System.out.println(code + " ");
                        Integer value6 = map.get(code);    //Inter 整数类
                        if (value6 != null) {
                            weightArray[r][c] += value6;
                        }
                        //往右
                        code = "0";
                        chess = 0;
                        number = 0;
                        for (i = r - 1, j = c + 1; i >= 0 && j < COLUM; i--, j++) {
                            if (chessArray[i][j] == 0) {
                                if (r == i + 1 && c == j - 1) {
                                    break;
                                } else if (number == 0) {
                                    code = code + chessArray[i][j];
                                    number++;
                                } else if (number == 1) {
                                    if (chessArray[i][j] == chessArray[i + 1][j - 1]) {
                                        break;
                                    } else {
                                        code = code + chessArray[i][j];
                                        number++;
                                    }
                                } else if (number == 2) {
                                    if (chessArray[i][j] == chessArray[i + 1][j - 1]) {
                                        break;
                                    }
                                }
                            } else {
                                if (chess == 0) {
                                    chess = chessArray[i][j];
                                    code = code + chessArray[i][j];
                                } else if (chess == chessArray[i][j]) {
                                    code = code + chessArray[i][j];
                                } else {
                                    code = code + chessArray[i][j];
                                    break;
                                }
                            }
                        }
                        System.out.println(code + " ");
                        Integer value7 = map.get(code);    //Inter 整数类
                        if (value7 != null) {
                            weightArray[r][c] += value7;
                        }
                    }
                }
            }
        }
    }
}
